package com.zhaosc.rmi;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

import com.zhaosc.loadbalance.NodeInfo;

public class RmiUtil {

	public void startRmiServer(String host, String port, String id) {

		try {
			SoaRmi soaRmi = new SoaRmiImpl();
			LocateRegistry.createRegistry(Integer.parseInt(port));

			Naming.bind("rmi://" + host + ":" + port + "/" + id, soaRmi);

			System.out.println("rmi server start !!!");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public SoaRmi startRmiClient(NodeInfo nodeInfo, String id) {

      String host=nodeInfo.getHost();
      String port=nodeInfo.getPort();
      
      try{
    	  return (SoaRmi) Naming.lookup("rmi://" + host + ":" + port + "/"
                  + id);
      }catch (MalformedURLException e) {
          e.printStackTrace();
      }
      catch (RemoteException e) {
          e.printStackTrace();
      }
      catch (NotBoundException e) {
          e.printStackTrace();
      }
      return null;
	}

}
